package com.nl.controller.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.bo.CreateGroupReq;
import com.nl.controller.web.bean.bo.UpdateGroupInfoReq;
import com.nl.controller.web.bean.po.mgrgrouppo.SmsMgrGroupInfo;
import com.nl.controller.web.mapper.SmsMgrGroupMapper;
import com.nl.controller.web.mapper.SmsUserMapper;
import com.nl.controller.web.mapper.UtilMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @desc 项目分组
 * @return null
 * @author l
 * @date 2021/9/13 16:54
 */
@Slf4j
@Service
public class SmsMgrGroupService {
    @Autowired
    private SmsMgrGroupMapper smsMgrGroupMapper;

    @Autowired
    private UtilMapper utilMapper;

    @Autowired
    private SmsUserMapper smsUserMapper;
    
    /**
     * @desc 查询项目分组列表
     * @param qryGroupListReq 
     * @return com.github.pagehelper.PageInfo<com.nl.controller.web.bean.po.mgrgrouppo.SmsMgrGroupInfo>
     * @author l
     * @date 2021/9/23 15:05
     */
    public PageInfo<SmsMgrGroupInfo> qryGroupList(int page,int limit,String cityId,String countyId,String groupName) {
        List<SmsMgrGroupInfo> list;
        try{
            PageHelper.startPage(page, limit);
            list = smsMgrGroupMapper.qryMgrGroupList(cityId,countyId,groupName);
            log.info("---"+list.toString());
            if(!CollectionUtils.isEmpty(list)){
                for(SmsMgrGroupInfo smsMgrGroupInfo:list){

                    String createOperatorId = smsMgrGroupInfo.getCreateOperator();
                    String createOperatorName = "暂无信息";
                    SmsUsers smsUsers = smsUserMapper.findUserByUserId(createOperatorId);
                    if(!Objects.isNull(smsUsers)){
                        smsMgrGroupInfo.setCreateOperator(smsUsers.getUserName());
                    }else{
                        smsMgrGroupInfo.setCreateOperator(createOperatorName);
                    }
                    if(null==smsMgrGroupInfo.getCityId()||"".equals(smsMgrGroupInfo.getCityId())||"25".equals(smsMgrGroupInfo.getCityId())){
                        smsMgrGroupInfo.setCityName("-");
                    }else{
                        smsMgrGroupInfo.setCityName(smsMgrGroupInfo.getCityName()+"市");
                    }
                    if(null==smsMgrGroupInfo.getCountyId()||"".equals(smsMgrGroupInfo.getCountyId())){
                        smsMgrGroupInfo.setCountyName("-");
                    }else{
                        smsMgrGroupInfo.setCountyName(utilMapper.getCountyName(smsMgrGroupInfo.getCountyId()));
                    }
                    smsMgrGroupInfo.setBelong("江苏省/"+smsMgrGroupInfo.getCityName()+"/"+smsMgrGroupInfo.getCountyName());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("项目分组列表查询出现异常：",e.getMessage());
            throw new SmsException("项目分组列表查询出现异常:"+e.getMessage());
        }
        PageInfo<SmsMgrGroupInfo> pageInfo = new PageInfo<SmsMgrGroupInfo>(list);
        return pageInfo;
    }
    
    /**
     * @desc 新增项目分组信息
     * @param createGroupReq 
     * @return com.nl.controller.common.model.Response
     * @author l
     * @date 2021/9/23 16:32
     */
    public Response createGroupInfo(CreateGroupReq createGroupReq) {

        Response response = new Response();
        try {
            String groupId = smsMgrGroupMapper.getGroupSeq();
            createGroupReq.setOperatorId(createGroupReq.getOperatorId());
            createGroupReq.setGroupId(groupId);
            int insertResult = smsMgrGroupMapper.createGroupInfo(createGroupReq);
            if(insertResult>0 && !CollectionUtils.isEmpty(createGroupReq.getRelMgrList())){
                smsMgrGroupMapper.createGroupMgrRel(createGroupReq);
            }
            response.setCode(0);
            response.setMsg("新增分组关联项目信息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error("新增分组关联项目信息发生异常：",e.getMessage());
            throw new SmsException("新增分组关联项目信息发生异常:"+e.getMessage());
        }
        return response;
    }

    public Response updateGroupInfo(UpdateGroupInfoReq updateGroupInfoReq) {
        String operatorId = updateGroupInfoReq.getOperatorId();
        Response response = new Response();
        try{
            updateGroupInfoReq.setOperatorId(operatorId);
            int updateResult = smsMgrGroupMapper.updateGroupInfo(updateGroupInfoReq);

            if(updateResult >0 && !CollectionUtils.isEmpty(updateGroupInfoReq.getRelMgrList())){
                //先删除原有的关联项目
                smsMgrGroupMapper.deleteRelMgr(updateGroupInfoReq.getGroupId());
                CreateGroupReq createGroupReq = new CreateGroupReq();
                BeanUtils.copyProperties(updateGroupInfoReq,createGroupReq);
                smsMgrGroupMapper.createGroupMgrRel(createGroupReq);
            }
            response.setCode(0);
            response.setMsg("变更分组关联项目信息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error("变更分组关联项目信息发生异常：",e.getMessage());
            throw new SmsException("变更分组关联项目信息发生异常:"+e.getMessage());
        }
        return response;
    }

    public Response qryGroupInfoById(String groupId) {
        Response response = new Response();
        try {
            log.info("-----"+groupId);
            SmsMgrGroupInfo smsMgrGroupInfo = smsMgrGroupMapper.qryGroupInfoById(groupId);
            List<String> mgrIdList = new ArrayList<>();
            List<String> mgrIdList2 = smsMgrGroupMapper.qryRelMgrList(groupId);
            if(CollectionUtils.isEmpty(mgrIdList2)){
                smsMgrGroupInfo.setRelMgrIdList(mgrIdList);
            }else{
                smsMgrGroupInfo.setRelMgrIdList(mgrIdList2);
            }

            response.setCode(0);
            response.setMsg("根据编码查询项目信息成功");
            response.setData(smsMgrGroupInfo);
        }catch (Exception e){
            e.printStackTrace();
            log.error("根据编码查询项目信息发生异常：",e.getMessage());
            throw new SmsException("根据编码查询项目信息发生异常:"+e.getMessage());
        }
        return  response;
    }

    public Response deleteMgrGroup(String groupId, String operatorId) {
        Response response = new Response();
        try {
            int deleteMgrGroupResult = smsMgrGroupMapper.deleteMgrGroup(groupId,operatorId);
            if(deleteMgrGroupResult > 0){
                smsMgrGroupMapper.deleteRelMgr(groupId);
            }
            response.setCode(0);
            response.setMsg("删除项目分组信息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除项目分组信息发生异常：",e.getMessage());
            throw new SmsException("删除项目分组信息发生异常:"+e.getMessage());
        }
        return  response;
    }

    public Response qryGroupList2(String cityId, String countyId) {
        List<SmsMgrGroupInfo> list;
        Response response = new Response();
        try{
            list = smsMgrGroupMapper.qryMgrGroupList(cityId,countyId,"");
            response.setCode(0);
            response.setMsg("查询项目分组列表成功");
            response.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            log.error("项目分组列表查询出现异常：",e.getMessage());
            throw new SmsException("项目分组列表查询出现异常:"+e.getMessage());
        }
        return response;
    }

    public Response judgeGroupNameRepeat(String groupName) {
        Response response = new Response();
        try{
            int count =  smsMgrGroupMapper.judgeGroupNameRepeat(groupName);
            response.setCode(0);
            response.setMsg("判断分组名称是否重复成功");
            response.setData(count);
        }catch (Exception e){
            e.printStackTrace();
            log.error("项目分组列表查询出现异常：",e.getMessage());
            throw new SmsException("判断分组名称是否重复出现异常:"+e.getMessage());
        }
        return response;
    }
}
